// 节流
export function throttle(fn, aply) {
  let open = true; // 开关 默认是开
  return (...ags) => {
    if (!open) return; // 如果节流函数已经执行了 直接return
    open = false; // 设为关
    setTimeout(() => {
      fn.apply(this, ags);
      open = true; // 节流周期执行完设为开
    }, aply);
  };
}
// 防抖
export function debounce(fn, laby) {
  let timer = null; // 定时器的返回值 初始为null
  return (...ags) => {
    clearTimeout(timer); // 如果防抖函数一直触发那么fn一直不执行
    timer = setTimeout(() => {
      fn.apply(this, ags);
    }, laby);
  };
}
/**
 *
 * @param {data||时间戳必须是13位||空} inputTime
 *  @returns {北京时间戳}
 */
export function getBeijingtime(res) {
  // 获得当前运行环境时间
  let d = new Date();
  let currentDate;
  if (res) {
    currentDate = new Date(res);
  } else {
    currentDate = new Date();
  }
  let tmpHours = currentDate.getHours();
  // 算得时区
  let time_zone = -d.getTimezoneOffset() / 60;
  if (time_zone < 0) {
    time_zone = Math.abs(time_zone) + 8;
    currentDate.setHours(tmpHours + time_zone);
  } else {
    time_zone -= 8;
    currentDate.setHours(tmpHours - time_zone);
  }
  return currentDate;
}
/**
 * 转化为时间字符串
 * @param {Date|number} date - Date 对象或时间戳(ms)
 * @param {boolean} [isFull=false] - 是否为完整日期时间
 * @param {string} [connector='-'] - 连接符
 * @returns {string} - yyyy-MM-dd
 */
export function formatDate(inputTime) {
  let ds = new Date();
  let currentDate = new Date(inputTime * 1000);
  let tmpHours = currentDate.getHours();
  // 算得时区
  let time_zone = -ds.getTimezoneOffset() / 60;
  if (time_zone < 0) {
    time_zone = Math.abs(time_zone) + 8;
    currentDate.setHours(tmpHours + time_zone);
  } else {
    time_zone -= 8;
    currentDate.setHours(tmpHours - time_zone);
  }
  // return currentDate;
  let date = currentDate;
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d = date.getDate();
  d = d < 10 ? "0" + d : d;
  let h = date.getHours();
  h = h < 10 ? "0" + h : h;
  let minute = date.getMinutes();
  let second = date.getSeconds();
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
}
// 十进制转二进制位操作
/**
 * @param {number} 十进制数据
 * @package 取第几位
 * @returns 第几位数据
 */
export function weiInt(arr = 100, i) {
  let data = arr
    .toString(2)
    .split("")
    .reverse()[i];
  if (data) {
    return Boolean(Number(data));
  } else {
    return false;
  }
}
/**
 * @param {Array}
 * @param {Array}
 * @param {Array}
 * @returns {Array}
 */
export function hebing(arr, num1, num2) {
  for (let i = 0; i < num1.length; i++) {
    let data = {};
    let wei = 0;
    let wei2 = 0;
    let data2 = {};
    arr.forEach(function (item, index) {
      if (item.code === num1[i]) {
        wei = index;
        data = item;
      }
    });
    arr.forEach(function (item, index) {
      if (item.code === num2[i]) {
        wei2 = index;
        data2 = item;
      }
    });
    if (wei && wei2) {
      data.code = data2.code + "," + data.code;
      data.value = Math.round(data.value * 65536 + data2.value);
      arr.splice(wei2, 1);
      arr[wei] = data;
    }
  }

  return arr;
}
// 合并数组
/**
 * @param {Array}
 * @param {Array}
 * @returns {Array}
 */
export function heB(arr, num1, num2, list) {
  for (let i = 0; i < num1.length; i++) {
    let wei = null;
    let wei2 = null;

    arr.forEach(function (item, index) {
      if (item === num1[i]) {
        wei = index;
        console.log(item);
        console.log(wei);

        // data = item;
      }
    });
    arr.forEach(function (item, index) {
      if (item === num2[i]) {
        wei2 = index;
        console.log(item);
        console.log(wei2);
        // data2 = item;
      }
    });
    if (typeof wei === "number" && typeof wei2 === "number") {
      let list1 = list[wei];
      let list2 = list[wei2];
      list1.map((item, index) => {
        list1[index] = Math.round(item * 65536 + list2[index]);
      });
      list[wei] = list1;
      list.splice(wei2, 1);
    }
  }

  return list;
}

// 发送指令 40001
export function cmdSet(a, b) {
  let arr = JSON.parse(localStorage.getItem("cmd"));
  // console.log(arr);
  // 10进制转2进制
  let arrCmd = arr[40001].toString(2);
  let num = [...arrCmd].reverse();
  // 判断当前位，小于自动补齐
  if (num.length < a) {
    for (let i = num.length; i < a + 1; i++) {
      num[i] = 0;
    }
    num[a] = b;
    arr[40001] = parseInt(num.reverse().join(""), 2);
    localStorage.setItem("cmd", JSON.stringify(arr));
  } else {
    // 不小于直接修改
    num[a] = b;
    arr[40001] = parseInt(num.reverse().join(""), 2);
    localStorage.setItem("cmd", JSON.stringify(arr));
  }
}
// 发送指令 80001
export function cmdSet1(a, b) {
  let arr = JSON.parse(localStorage.getItem("cmd1"));
  // console.log(arr);
  // 10进制转2进制
  let arrCmd = arr[80001].toString(2);
  let num = [...arrCmd].reverse();
  // 判断当前位，小于自动补齐
  if (num.length < a) {
    for (let i = num.length; i < a + 1; i++) {
      num[i] = 0;
    }
    num[a] = b;
    arr[80001] = parseInt(num.reverse().join(""), 2);
    localStorage.setItem("cmd", JSON.stringify(arr));
  } else {
    // 不小于直接修改
    num[a] = b;
    arr[80001] = parseInt(num.reverse().join(""), 2);
    localStorage.setItem("cmd", JSON.stringify(arr));
  }
}
